<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace crcl\base\template;

interface IValues
{
  public function set($key, $value);
  public function get($key);
  public function getFalse($key);
  public function out($sFrom, $sFilter = \FALSE);
  public function toArray();
}

class Values implements IValues
{
  public function set($key, $value)
  {
    $this->aValues[$key] = $value;
  }

  public function merge($aValues)
  {
    foreach ($aValues as $key => $value)
    {
      $this->set($key, $value);
    }
  }

  public function get($key)
  {
    $iPos = \strpos($key, '->');
    if ($iPos !== \FALSE)
    {
      $sKey = \substr($key, 0, $iPos);
      $oKey = $this->aValues[$sKey];
      $return = $oKey->{\substr($key, $iPos+2)};
      if ($return instanceof \crcl\base\structure\field\IConcrete)
      {
        $return = $return->get();
      }
      return $return;
    }
    $iPos = \strpos($key, '[');
    if ($iPos !== \FALSE)
    {
      $sKey = \substr($key, 0, $iPos);
      $aKey = $this->aValues[$sKey];
      $sParam = \substr($key, $iPos+1);
      $sParam = \substr($sParam, 0, strlen($sParam)-1);
      if (isset($aKey[$sParam]))
      {
        $return = $aKey[$sParam];
        if ($return instanceof \crcl\base\structure\field\IConcrete)
        {
          $return = $return->get();
        }
        return $return;
      }
    }
    if (isset($this->aValues[$key]))
    {
      return $this->aValues[$key];
    }
    else
    {
      throw new Exception('Missing value', Exception::ERROR_MISSINGVALUE,
        array('key' => $key, 'values' => array_keys($this->aValues)));
    }
  }

  public function getFalse($key)
  {
    try
    {
      return $this->get($key);
    }
    catch (Exception $e)
    {
      return \FALSE;
    }
  }

  public function out($sFrom, $sFilter = \FALSE)
  {
    \extract($this->aValues);

    \crcl\base\exception\Handler::resetLastSilentError();

    if ($sFilter)
    {
      $sOut = @eval('return \crcl\base\Filter::getInstance(\'' . $sFilter . '\')->filter("' . $sFrom . '");');
    }
    else
    {
      $sOut = @eval('return "' . $sFrom . '";');
    }

    $oError = \crcl\base\exception\Handler::getLastSilentError();
    if (!\is_null($oError) && ($oError->getMessage() !== ''))
    {
      if (\strpos($oError->getMessage(), 'Undefined variable:') !== \FALSE)
      {
        $sMessage = \str_replace('Undefined variable', 'Missing value', $oError->getMessage());
        throw new Exception($sMessage, Exception::ERROR_MISSINGVALUE,
          array('from' => $sFrom, 'values' => $this->aValues, 'error' => $oError->getMessage()));
      }
      else
      {
        trigger_error($oError->getMessage());
      }
    }
    $sOut = \crcl\base\string\Entities::numeric($sOut);
    return $sOut;
  }

  public function toArray()
  {
    return $this->aValues;
  }

  /** PRIVATE *************************************************************************************/

  private $aValues = array();
}

//filter integrieren
//factory über typen? nicht notwendig, weil im tpl alles strings?
//format checks? zumindest bei array/obj
